//  HomeVC.m
//  WanCaiC8
//
//  Created by tangang on 2017/12/29.
//  Copyright © 2017年 tangang. All rights reserved.
//
#import "HomeVC.h"
#import "RankingList.h"
#import "HomeInformationVC.h"
#import "MasterRankVC.h"
#import "RegisteredVC.h"
#import "HomePageVM.h"
#import "PlanVC.h"
#import "OpenLotteryRecordVC.h"
#import "JoinQuizDetailVC.h"
#import "Home_PlanVC.h"
#import "MyVC.h"

#import "AdvertisingTextCell.h"

#import "OpenLotteryInfoCollectionViewCell.h"

#import "InformationVC.h"

#import "TGInformationViewController.h"

/******/
#import "LabCell.h"
#import "LotteryTopView.h"
#import "PredictVC.h"
#import "InformationVM.h"
#import "HomeTypeCell.h"
#import "LotteryHeadTitleView.h"
#import "AdvertisingView.h"

#import "AdvertisingNoImageCell.h"
#import "AdvertisingBigImageCell.h"
#import "AdvertisingSmallImageCell.h"
#import "AdvertisingMoreImageCell.h"
#import "ImageCell.h"
#import "PlanVC.h"
#import "MasterListVC.h"
#import "InfoTypeView.h"
#import "NewsMasterCell.h"
#import "PlanPeopleVC.h"
#import "LotteryPeopleHeadView.h"
#import "LotteryLayout.h"
#import "SocketRocketUtility.h"


#import "TGPlanViewController.h"
#import "TGMasterListViewController.h"
#import "TGTrendListViewController.h"

#import "TGAdvertCollectionViewCell.h"

@interface HomeVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *collectionView;

@property (nonatomic ,copy)   void (^selectType)(NewsTypeModel *);
///新闻图库列表
@property (nonatomic ,strong) NSMutableArray *newsPicArr;
///新闻栏目列表
@property (nonatomic ,strong) NSMutableArray *newsTypeArr;

@property (nonatomic ,strong) NSArray        *advertisingArr;

@property (nonatomic ,strong) NSMutableArray         *peopleArr;
@property (nonatomic ,strong) NSArray                 *dataArr;
@property (nonatomic ,strong) NSMutableArray                 *navArr;

/**
 新增的高手排行榜 数组
 */
@property (nonatomic ,strong) NSArray                 *palyInfoArr;


@end

@implementation HomeVC{
    UIButton    *btnHigh;
    UIButton    *btnNational;
    NSInteger    selectLottery;
    dispatch_source_t _timer;
    

    NSInteger   selectNewsType;
    NSInteger   page;
    NSMutableArray *arr1;
    NSMutableArray *arr2;
    NSMutableArray *arr3;
    NSMutableArray *arr4;
    NSMutableArray *arr5;
    NSMutableArray *arr6;
    NSMutableArray *arr7;
    NSMutableArray *arr8;
    NSMutableArray *arr9;
    NSMutableArray *arr10;
    
    //记录isdisplay为1 的数组
    NSMutableArray *colArr1;
    NSMutableArray *colArr2;
    NSMutableArray *colArr3;
    NSMutableArray *colArr4;
    NSMutableArray *colArr5;
    NSMutableArray *colArr6;
    NSMutableArray *colArr7;
    NSMutableArray *colArr8;
    NSMutableArray *colArr9;
    NSMutableArray *colArr10;

}


#pragma mark -- 获取彩票分类
- (void)getLotteryType{
    WS(weakSelf);
    [[InformationVM manage] getNewsLotteryTypeListWithSuccess:^(id returnValue) {
       
//        NewsLottryTypeModel *model=[InformationVM manage].lotteryTypeArr[i];
        
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

#pragma mark - 加载数据
-(void)loadNavData{
    
    [[HomePageVM manage] getNavigationByLhcSuccess:^(id returnValue) {
        
        self.navArr = returnValue;
        
        [self.collectionView reloadData];
        
    } Fail:^(NSString *errorMessage, NSError *error) {
        
    }];
}


#pragma mark -- 获取新闻栏目列表
- (void)getNewsList{
    WS(weakSelf);
    [[InformationVM manage] getNewsTypeListWithId:@"5" Success:^(id returnValue) {
        
        [weakSelf createData:returnValue];
        
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)createData:(NSMutableArray *)data{
    self.dataArr=data;
    [self.newsPicArr removeAllObjects];
    [self.newsTypeArr removeAllObjects];
    for (NewsTypeModel *model in data) {
        NSLog(@"%@",model.TypeName);
        if (model.GroupType==0) {
            if (!([model.TypeName isEqualToString:@"香港挂牌"]||[model.TypeName isEqualToString:@"跑狗玄机"])) {
                [self.newsTypeArr addObject:model];
            }
            
        }else{
            [self.newsPicArr addObject:model];
        }
    }
    [self.collectionView reloadData];
    [self getAdvertising];
}

-(void)setupLeftItem{
    
    
    UIImageView *leftImageV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"leftIcon"]];
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithCustomView:leftImageV];
    self.navigationItem.leftBarButtonItem=leftItem;

}




- (float)getViewHeight:(AdvertisingModel *)model{
    float height=0;
    if (model.ThumbStyle==0) {
        height =[TGTools getSpaceLabelHeight:model.Title withFont:[UIFont systemFontOfSize:real_size(15)] withWidth:SCREEN_WIDTH-27 andLineSpacing:5]+55;
        height-=33;
    }else if (model.ThumbStyle==1){
        height =[TGTools getSpaceLabelHeight:model.Title withFont:[UIFont systemFontOfSize:real_size(15)] withWidth:SCREEN_WIDTH-27-125 andLineSpacing:5]+55;
        
        height-=33;
        
        if (height<95) {
            height=95;
        }
        
    }else if (model.ThumbStyle==2){
        height =[TGTools getSpaceLabelHeight:model.Title withFont:[UIFont systemFontOfSize:real_size(15)] withWidth:SCREEN_WIDTH-27 andLineSpacing:5]+50+(SCREEN_WIDTH-22)/394.0*160;
        height-=33;
    }else if (model.ThumbStyle==3){
        height =[TGTools getSpaceLabelHeight:model.Title withFont:[UIFont systemFontOfSize:real_size(15)] withWidth:SCREEN_WIDTH-27 andLineSpacing:5]+50+(SCREEN_WIDTH-80)/3.0/105*75;
        height-=33;
    }else if (model.ThumbStyle==4){
        height=(SCREEN_WIDTH-22)/394.0*160+10;
    }
    
    return height;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=DEFAULTBGCOLOR;
    

    
   
//    [HomePageVM manage].homeVC=self;
    
    [self setupLeftItem];
    
    [self setupUI];

    
    
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{

//        [self getAdvertising];
        [self checkVersion];
        
        [self getNewsList];
        [self getPlayInfo];
        
        [self loadNavData];
        
        [self getLotteryType];
        
    });

}
#pragma mark - 新增6彩高手排行榜

-(void)getPlayInfo{
    WS(weakSelf);
    [[InformationVM manage] getPlayInfoWithlType:@"5" Success:^(id returnValue) {
        
        weakSelf.palyInfoArr = [NSArray arrayWithArray:returnValue];
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self getNewsList];
        });
        
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
    
}

-(void)checkVersion{
    
    WS(weakSelf);
    [[HomePageVM manage] getVersionWithId:UpdateKey andSuccess:^(id returnValue) {
        NSString *strUrl=[returnValue objectForKey:@"Downurl"];
        if (strUrl.length>0) {
            [weakSelf checkVersion:returnValue];
        }
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)checkVersion:(NSDictionary *)dict{
    switch ([[dict objectForKey:@"Status"] integerValue]) {
        case 2:
            [self showAlert:2 andContent:[dict objectForKey:@"Content"] andUrl:[dict objectForKey:@"Downurl"]];
            break;
        case 3:
            [self showAlert:3 andContent:[dict objectForKey:@"Content"] andUrl:[dict objectForKey:@"Downurl"]];
            break;
        default:
            break;
    }
}

- (void)showAlert:(NSInteger )status andContent:(NSString *)content andUrl:(NSString *)url{
    NSString *Title = @"更新提示";
    content=[content stringByReplacingOccurrencesOfString:@"\\r" withString:@"\r"];
    content=[content stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:Title message:content preferredStyle:UIAlertControllerStyleAlert];
    if (status==2) {
        UIAlertAction *cancleAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: url]];
            exit(0);
        }];
        [alertController addAction:sureAction];
        [alertController addAction:cancleAction];
    }else if (status==3){
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: url]];
            exit(0);
        }];
        [alertController addAction:sureAction];
    }
    [self presentViewController:alertController animated:YES completion:nil];
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [BaseVM manage].viewCtrl=self;
    self.hidesBottomBarWhenPushed = NO;
    self.tabBarController.tabBar.hidden = NO;
    self.navigationItem.rightBarButtonItems=nil;
    self.navigationItem.rightBarButtonItem=nil;
    if ([UserModel userModel].UserId.length>0) {
        UIBarButtonItem *loginItem=[[UIBarButtonItem alloc]initWithTitle:[UserModel userModel].NickName style:UIBarButtonItemStylePlain target:self action:@selector(onclickHead)];
        UIBarButtonItem *regisItem=[[UIBarButtonItem alloc]initWithTitle:@"退出" style:UIBarButtonItemStylePlain target:self action:@selector(onclickExit)];
        self.navigationItem.rightBarButtonItems=@[regisItem,loginItem];
//        UIImageView *headImageV=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
//        headImageV.layer.masksToBounds=YES;
//        headImageV.layer.cornerRadius=15.0;
//        [headImageV sd_setImageWithURL:[NSURL URLWithString:[UserModel userModel].Avater]];
//        headImageV.userInteractionEnabled=YES;
//        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
//        [view addSubview:headImageV];
//        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickHead)];
//        [headImageV addGestureRecognizer:tap];
//        UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithCustomView:view];
//        self.navigationItem.rightBarButtonItem=rightItem;
    }else{
        UIBarButtonItem *loginItem=[[UIBarButtonItem alloc]initWithTitle:@"登录 " style:UIBarButtonItemStylePlain target:self action:@selector(onclickLogin)];
        UIBarButtonItem *regisItem=[[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(onclickRegis)];
        self.navigationItem.rightBarButtonItems=@[loginItem,regisItem];
    }

}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
}



#pragma mark -- 登录注册
- (void)onclickLogin{
    [[BaseVM manage] login];
}

- (void)onclickRegis{
    RegisteredVC *vc=[[UIStoryboard storyboardWithName:@"CLogin" bundle:nil]instantiateViewControllerWithIdentifier:@"RegisteredVC"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onclickExit{
    [UserModel clean];
    UIBarButtonItem *loginItem=[[UIBarButtonItem alloc]initWithTitle:@"登录 " style:UIBarButtonItemStylePlain target:self action:@selector(onclickLogin)];
    UIBarButtonItem *regisItem=[[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(onclickRegis)];
    self.navigationItem.rightBarButtonItems=@[loginItem,regisItem];
}

#pragma mark -- 获取广告
- (void)getAdvertising{
//    if ([BaseVM manage].city.length>0) {
   
    

        WS(weakSelf);
        [[InformationVM manage] getAdvertWithLocation:@"0" andType:4 Success:^(id returnValue) {
            
            
            arr1=[[NSMutableArray alloc]init];
            arr2=[[NSMutableArray alloc]init];
            arr3=[[NSMutableArray alloc]init];
            arr4=[[NSMutableArray alloc]init];
            arr5=[[NSMutableArray alloc]init];
            arr6=[[NSMutableArray alloc]init];
            arr7=[[NSMutableArray alloc]init];
            arr8=[[NSMutableArray alloc]init];
            arr9=[[NSMutableArray alloc]init];
            arr10=[[NSMutableArray alloc]init];
            colArr1 =  [NSMutableArray array];
            colArr2 =  [NSMutableArray array];
            colArr3 =  [NSMutableArray array];
            colArr4 =  [NSMutableArray array];
            colArr5 =  [NSMutableArray array];
            colArr6 =  [NSMutableArray array];
            colArr7 =  [NSMutableArray array];
            colArr8 =  [NSMutableArray array];
            colArr9 =  [NSMutableArray array];
            colArr10 =  [NSMutableArray array];
            
            for (AdvertisingModel *model in returnValue) {
                
              
                if (model.Display) {
                    if (model.Layer==1) {
                        [arr1 addObject:model];
                        if (model.IsDisplay) {
                            [colArr1 addObject:model];
                        }
                    }else if (model.Layer==2){
                        [arr2 addObject:model];
                        if (model.IsDisplay) {
                            [colArr2 addObject:model];
                        }
                    }else if (model.Layer==3){
                        [arr3 addObject:model];
                        if (model.IsDisplay) {
                            [colArr3 addObject:model];
                        }
                    }else if (model.Layer==4){
                        [arr4 addObject:model];
                        if (model.IsDisplay) {
                            [colArr4 addObject:model];
                        }
                    }else if (model.Layer==5){
                        [arr5 addObject:model];
                        if (model.IsDisplay) {
                            [colArr5 addObject:model];
                        }
                    }else if (model.Layer==6){
                        [arr6 addObject:model];
                        if (model.IsDisplay) {
                            [colArr6 addObject:model];
                        }
                    }else if (model.Layer==7){
                        [arr7 addObject:model];
                        if (model.IsDisplay) {
                            [colArr7 addObject:model];
                        }
                    }else if (model.Layer==8){
                        [arr8 addObject:model];
                        if (model.IsDisplay) {
                            [colArr8 addObject:model];
                        }
                    }else if (model.Layer==9){
                        [arr9 addObject:model];
                        if (model.IsDisplay) {
                            [colArr9 addObject:model];
                        }
                    }else if (model.Layer==10){
                        [arr10 addObject:model];
                        if (model.IsDisplay) {
                            [colArr10 addObject:model];
                        }
                    }
                }
            }
            
            [self.collectionView reloadData];
     
       
            
        } andFail:^(NSString *errorMessage, NSError *error) {
            
        }];
//    }
}



#pragma mark -- 点击头像
- (void)onclickHead{
    MyVC *vc=[[MyVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - setupUI
-(void)setupUI{
    
    LotteryLayout *layout = [[LotteryLayout alloc] init];
    layout.cellType=AlignWithLeft;
    [layout setItemSize:CGSizeMake(floor(SCREEN_WIDTH/3.0) , 40)];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.minimumInteritemSpacing = 0.0;
    layout.minimumLineSpacing = 0.0;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor=[UIColor whiteColor];
    _collectionView.showsVerticalScrollIndicator=NO;
    
    
     [_collectionView registerClass:[TGAdvertCollectionViewCell class] forCellWithReuseIdentifier:@"TGAdvertCollectionViewCell"];
    
    [_collectionView registerClass:[OpenLotteryInfoCollectionViewCell class] forCellWithReuseIdentifier:@"OpenLotteryInfoCollectionViewCell"];
    
    [_collectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
    [_collectionView registerClass:[HomeTypeCell class] forCellWithReuseIdentifier:@"HomeTypeCell"];
    
    
    [_collectionView registerClass:[AdvertisingMoreImageCell class] forCellWithReuseIdentifier:@"AdvertisingMoreImageCell"];
    [_collectionView registerClass:[AdvertisingNoImageCell class] forCellWithReuseIdentifier:@"AdvertisingNoImageCell"];
    [_collectionView registerClass:[AdvertisingBigImageCell class] forCellWithReuseIdentifier:@"AdvertisingBigImageCell"];
    [_collectionView registerClass:[AdvertisingSmallImageCell class] forCellWithReuseIdentifier:@"AdvertisingSmallImageCell"];
    
    [_collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
    
//    [_collectionView registerClass:[LotteryHeadTitleView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LotteryHeadTitleView"];
    [_collectionView registerClass:[LotteryPeopleHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LotteryPeopleHeadView"];
    
    [self.view addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

}



#pragma mark - collectionView代理函数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 6;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==2) {
        return  arr4.count?1:0;
    }else if (section==4){
        return self.newsPicArr.count+(arr8.count?1:0);
    }else if (section == 3){
        return self.palyInfoArr.count +(arr6.count?1:0);
    }else if (section==0){
        return self.navArr.count+(arr1.count?1:0);
    }else if (section ==1){
        return 1;
    }
    return self.newsTypeArr.count+(arr10.count?1:0);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==2) {
        
        TGAdvertCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TGAdvertCollectionViewCell" forIndexPath:indexPath];
        
        cell.dataArr = arr4;
        
        return cell;
        
    }else if (indexPath.section==3){
        
        if (indexPath.row<self.palyInfoArr.count) {
            LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
            PlayRuleModel *model=self.palyInfoArr[indexPath.row];
            [cell setLineTitle:model.Name];
            return cell;
        } else {
            
            
            TGAdvertCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TGAdvertCollectionViewCell" forIndexPath:indexPath];
            
            cell.dataArr = arr6;
            
            return cell;
            
            
            
        }
        
        
    }else if (indexPath.section == 4){
        
        if (indexPath.row<self.newsPicArr.count) {
            LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
            NewsTypeModel *model=self.newsPicArr[indexPath.row];
            [cell setLineTitle:model.TypeName];
            return cell;
        }else{
            
            TGAdvertCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TGAdvertCollectionViewCell" forIndexPath:indexPath];
            
            cell.dataArr = arr8;
            
            return cell;
        }
    }else if (indexPath.section ==5){
        if (indexPath.row<self.newsTypeArr.count) {
            LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
            NewsTypeModel *model=self.newsTypeArr[indexPath.row];
            [cell setLineTitle:model.TypeName];
            return cell;
        }else{
            TGAdvertCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TGAdvertCollectionViewCell" forIndexPath:indexPath];
            
            cell.dataArr = arr10;
            
            return cell;
        }
    }else if (indexPath.section==0){
        
        if (indexPath.row < self.navArr.count) {
            HomeTypeCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTypeCell" forIndexPath:indexPath];
            
            HomeNavigationByLhcModel *model =self.navArr[indexPath.row];
            [cell setHomeNavigationByLhcModel:model];
            
            return cell;
        }else{
            
            TGAdvertCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TGAdvertCollectionViewCell" forIndexPath:indexPath];
            
            cell.dataArr = arr1;
            
            return cell;
        }
        

    }else{
        
        OpenLotteryInfoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OpenLotteryInfoCollectionViewCell" forIndexPath:indexPath];

        return cell;
    }
    
}

- (UICollectionViewCell *)cellWithModel:(AdvertisingModel *)model andIndexPath:(NSIndexPath *)indexPach{
    if (model.ThumbStyle==0) {
        AdvertisingNoImageCell *cell=[self.collectionView dequeueReusableCellWithReuseIdentifier:@"AdvertisingNoImageCell" forIndexPath:indexPach];
        [cell setModel:model];
        [cell showLine:indexPach.row==0];
        return cell;
    }else if (model.ThumbStyle==1){
        AdvertisingSmallImageCell *cell=[self.collectionView dequeueReusableCellWithReuseIdentifier:@"AdvertisingSmallImageCell" forIndexPath:indexPach];
        [cell setModel:model];
        return cell;
        
    }else if (model.ThumbStyle==2){
        AdvertisingBigImageCell *cell=[self.collectionView dequeueReusableCellWithReuseIdentifier:@"AdvertisingBigImageCell" forIndexPath:indexPach];
        [cell setModel:model];
        return cell;
    }else if (model.ThumbStyle==3){
        AdvertisingMoreImageCell *cell=[self.collectionView dequeueReusableCellWithReuseIdentifier:@"AdvertisingMoreImageCell" forIndexPath:indexPach];
        [cell setModel:model];
        return cell;
    }else{
        ImageCell *cell=[self.collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPach];
        if (model.ThumbList.count>0) {
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.ThumbList[0]] placeholderImage:[UIImage imageNamed:@"big_loding"]];
        }else{
            cell.imageV.image=[UIImage imageNamed:@"big_loding"];
        }
        return cell;
    }
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==2) {
        if (arr4.count) {
            return CGSizeMake(SCREEN_WIDTH, 10 +arr4.count*40-ceil(colArr4.count/2.0)*40);

        }else{
            return CGSizeMake(SCREEN_WIDTH, arr4.count*40-ceil(colArr4.count/2.0)*40);
        }
        
    }else if (indexPath.section==4){
        if (indexPath.row<self.newsPicArr.count) {
            return CGSizeMake(SCREEN_WIDTH/3.0, 40);
        }else{
            return CGSizeMake(SCREEN_WIDTH, 10+arr8.count*40 - ceil(colArr8.count/2.0)*40);
        }
    }else if (indexPath.section==3){
        if (indexPath.row<self.palyInfoArr.count) {
            return CGSizeMake(SCREEN_WIDTH/4.0, 40);
        }
        else{
            return CGSizeMake(SCREEN_WIDTH, 10+arr6.count*40 - ceil(colArr6.count/2.0)*40);
        }
    }else if (indexPath.section ==5){
        if (indexPath.row<self.newsTypeArr.count) {
            return CGSizeMake(floor(SCREEN_WIDTH/3.0) , 40);
        }else{
             return CGSizeMake(SCREEN_WIDTH, 10+arr10.count*40 - ceil(colArr10.count/2.0)*40);
        }
    }else if (indexPath.section ==0){
        
        if (indexPath.row<self.navArr.count) {
            return CGSizeMake(SCREEN_WIDTH/4.0, SCREEN_WIDTH/5.0);
        } else {
           
            return CGSizeMake(SCREEN_WIDTH, 10+arr1.count*40 - ceil(colArr1.count/2.0)*40);
        }
        
       
    }else{
        return CGSizeMake(SCREEN_WIDTH, 138);
    }
    
    
}


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section ==0 ||section ==1) {
        return CGSizeZero;
    }else if (section==2) {
        if (!arr2.count) {
             return CGSizeMake(SCREEN_WIDTH, 40+40*(NSInteger)(arr2.count-ceil(colArr2.count/2.0)) +real_size(140));
        }
         return CGSizeMake(SCREEN_WIDTH, 40+15+40*(NSInteger)(arr2.count-ceil(colArr2.count/2.0)) +real_size(140));
       
    }
    return CGSizeMake(SCREEN_WIDTH, 45);
}

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionReusableView *reusableView = nil;
    
    if (kind == UICollectionElementKindSectionHeader)
    {
        if (indexPath.section==2) {
            LotteryPeopleHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LotteryPeopleHeadView" forIndexPath:indexPath];
           
            if (arr2.count) {
                
                [headerView setAdModelArr:arr2 AndRowCount:(NSInteger)(arr2.count-ceil(colArr2.count/2.0))];
                
            }
            if (arr3.count) {
                headerView.btnAdModelArr = arr3;
            }
            
            
            return headerView;
        }
        
        NSString *str = [NSString stringWithFormat:@"LotteryHeadTitleView%ld",indexPath.section];
        [_collectionView registerClass:[LotteryHeadTitleView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:str];
        LotteryHeadTitleView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:str forIndexPath:indexPath];
        
        if (indexPath.section==4) {
            [headerView setTitle:@"正版图库"];
            headerView.adModelArr = arr7;
        }else if (indexPath.section == 3){
            
            [headerView setTitle:@"高手排行榜"];
            headerView.adModelArr = arr5;
            
        }else if(indexPath.section ==5){
            [headerView setTitle:@"免费资料"];
            headerView.adModelArr = arr9;
        }
        reusableView = headerView;
    }
    return reusableView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==2) {
        AdvertisingModel *model=arr2[indexPath.row];
        
        if ([model.Title containsString:@"】"]) {
            
            UIPasteboard *copy = [UIPasteboard generalPasteboard];
            NSRange startRange = [model.Title rangeOfString:@"【"];
            NSRange endRange = [model.Title rangeOfString:@"】"];
            NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
            NSString *result = [model.Title substringWithRange:range];
            
            [copy setString:result];
            
            [SVProgressHUD showSuccessWithStatus:@"复制成功"];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString: @"weixin://"]];
            });
            
        }else{
            if (model.TransferUrl.length<1) {
                return;
            }
            
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: model.TransferUrl]];
            
        }
    }
    else if (indexPath.section == 3){
        
        if (indexPath.row<self.palyInfoArr.count) {
    
            TGMasterListViewController *vc =[[TGMasterListViewController alloc] init];
            vc.tempPlayRuleModel = self.palyInfoArr[indexPath.row];
            [self.navigationController pushViewController:vc animated:YES];

        } else {
            
            AdvertisingModel *model=arr4[indexPath.row-self.palyInfoArr.count];
            if (model.TransferUrl.length<1) {
                return;
            }
            if ([model.Title containsString:@"】"]) {
                
                UIPasteboard *copy = [UIPasteboard generalPasteboard];
                NSRange startRange = [model.Title rangeOfString:@"【"];
                NSRange endRange = [model.Title rangeOfString:@"】"];
                NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
                NSString *result = [model.Title substringWithRange:range];
                
                [copy setString:result];
                
                [SVProgressHUD showSuccessWithStatus:@"复制成功"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                    [[UIApplication sharedApplication]openURL:[NSURL URLWithString: @"weixin://"]];
                });
                
            }else{
                
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString: model.TransferUrl]];
                
            }
            
            
        }
        
        
        
        
    }
    
    else if (indexPath.section==4){
        if (indexPath.row<self.newsPicArr.count) {
            NewsTypeModel *model=self.newsPicArr[indexPath.row];
            for (int i=0; i<self.dataArr.count; i++) {
                NewsTypeModel *tModel=self.dataArr[i];
                if ([model.Id isEqualToString:tModel.Id]) {
                    model.index=i;
                    break;
                }
            }
        
            InformationVC *vc=[[InformationVC alloc]init];
            vc.saveViewController=self;
            [vc setLotteryWithType:0 andIndex:model.index];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            AdvertisingModel *model=arr6[indexPath.row-self.newsPicArr.count];
            if (model.TransferUrl.length<1) {
                return;
            }
            if ([model.Title containsString:@"】"]) {
                
                UIPasteboard *copy = [UIPasteboard generalPasteboard];
                NSRange startRange = [model.Title rangeOfString:@"【"];
                NSRange endRange = [model.Title rangeOfString:@"】"];
                NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
                NSString *result = [model.Title substringWithRange:range];
                
                [copy setString:result];
                
                [SVProgressHUD showSuccessWithStatus:@"复制成功"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                    [[UIApplication sharedApplication]openURL:[NSURL URLWithString: @"weixin://"]];
                });
                
            }else{
                
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString: model.TransferUrl]];
                
            }
            //            PublicWebVC *vc=[[PublicWebVC alloc]init];
            //            vc.url=model.TransferUrl;
            //            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }else if (indexPath.section==5) {
        if (indexPath.row<self.newsTypeArr.count) {
            NewsTypeModel *model=self.newsTypeArr[indexPath.row];
            for (int i=0; i<self.dataArr.count; i++) {
                NewsTypeModel *tModel=self.dataArr[i];
                if ([model.Id isEqualToString:tModel.Id]) {
                    model.index=i;
                    break;
                }
            }
            
            InformationVC *vc=[[InformationVC alloc]init];
            vc.saveViewController=self;
            [vc setLotteryWithType:0 andIndex:model.index];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else{
            AdvertisingModel *model=arr8[indexPath.row-self.newsTypeArr.count];
            if (model.TransferUrl.length<1) {
                return;
            }
            if ([model.Title containsString:@"】"]) {
                
                UIPasteboard *copy = [UIPasteboard generalPasteboard];
                NSRange startRange = [model.Title rangeOfString:@"【"];
                NSRange endRange = [model.Title rangeOfString:@"】"];
                NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
                NSString *result = [model.Title substringWithRange:range];
                
                [copy setString:result];
                
                [SVProgressHUD showSuccessWithStatus:@"复制成功"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                    [[UIApplication sharedApplication]openURL:[NSURL URLWithString: @"weixin://"]];
                });
                
            }else{
                
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString: model.TransferUrl]];
                
            }
        }
    }else if (indexPath.section==0){
        
        if (indexPath.row == 0) {
            RankingList *vc=[[RankingList alloc]init];
            vc.type=indexPath.row;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1){
            
            InformationVC *vc=[[InformationVC alloc]init];
            vc.saveViewController=self;
           
            vc.title = @"六合彩资讯";
            
            [vc setLotteryWithType:5 andIndex:61];
            [self.navigationController pushViewController:vc animated:YES];
            
            
        }else if (indexPath.row ==2){
            
            InformationVC *vc=[[InformationVC alloc]init];
            vc.saveViewController=self;
            [vc setLotteryWithType:4 andIndex:62];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if (indexPath.row == 3){
            TGTrendListViewController * vc = [[TGTrendListViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 4){
            TGPlanViewController *vc =[[TGPlanViewController alloc] init];
//            PlanVC *vc=[[PlanVC alloc]init];
//            LotteryTypeModel *typeModel=[[LotteryTypeModel alloc]init];
////            typeModel.lType=model.LType;
////            typeModel.Name=model.LTypeName;
////            typeModel.Id=model.BigLType;
//            vc.lotteryModel=typeModel;
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if (indexPath.row == 5){
            
            TGMasterListViewController *vc=[[TGMasterListViewController alloc]init];
   
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if (indexPath.row == 6){
            
            
            TGInformationViewController *vc=[[TGInformationViewController alloc]init];
            vc.type = TGInformationTypeInformation;

            [self.navigationController pushViewController:vc animated:YES];
            
        }else if(indexPath.row == 7){
            TGInformationViewController *vc=[[TGInformationViewController alloc]init];
            vc.type = TGInformationTypePicture;
            [self.navigationController pushViewController:vc animated:YES];
            
        }
        
    }
}


- (NSMutableArray *)newsTypeArr{
    if (!_newsTypeArr) {
        _newsTypeArr=[[NSMutableArray alloc]init];
    }
    return _newsTypeArr;
}

- (NSMutableArray *)newsPicArr{
    if (!_newsPicArr) {
        _newsPicArr=[[NSMutableArray alloc]init];
    }
    return _newsPicArr;
}

- (NSMutableArray *)navArr{
    if (!_navArr) {
        _navArr=[[NSMutableArray alloc]init];
    }
    return _navArr;
}




@end
